// pages/menu/commodity-price/price-detail/price-detail.js
var app = getApp()
var verify = require('../../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      commodityPriceId: options.commodityPriceId,
      productId: options.productId,
      commodityPriceName: options.commodityPriceName
    })
  },

  getPriceDetail(){
    var commodityPriceId = this.data.commodityPriceId

    var token = app.globalData.token
    var baseUrl = app.globalData.baseUrl
    var that = this
    wx.request({
      url: baseUrl + 'commodity_price/' + commodityPriceId,
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var priceDetail = res.data.data
          console.log("priceDetail = ", priceDetail)
          that.setData({
            priceDetail: priceDetail
          })
          

        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },

  // 修改价格
  editPrice(res){
    var productId = this.data.productId
    var commodityPriceName = this.data.commodityPriceName
    var priceDetail = this.data.priceDetail
    var commodityPriceId = priceDetail.commodityPriceId
    var index = res.currentTarget.dataset.index;
    var commodityPriceDetail = priceDetail.commodityPriceDetail;
    commodityPriceDetail = JSON.stringify(commodityPriceDetail);
    wx.navigateTo({
      url: 'edit-price/edit-price?commodityPriceDetail=' + commodityPriceDetail 
          + '&productId=' + productId
        + '&commodityPriceName=' + commodityPriceName
        + '&commodityPriceId=' + commodityPriceId+'&ind='+index,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    this.getPriceDetail()
  },
})